package com.seio.edc.controller.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.system.*;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.energy.SummaryService;
import com.nova.edc.service.system.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.service.energy.EnergyService;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.StringUtil;

/**
 * <p/>
 * Project Name: EMS智能节能监测平台 <br>
 * Description: 登录成功后的主页面
 */
@Controller
@RequestMapping(value = "/statistics")
public class StatisticsController {

    @Autowired
    private EnergyService novaEnergyService;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private CollectAreaService novaCollectAreaService;
    @Autowired
    private AreaPointService novaAreapointServices;
    @Autowired
    private RoleService novaRoleService;
    @Autowired
    private SummaryService novaSummaryService;
    @Autowired
    private GatherSummaryService novaGatherSummaryService;
    @Autowired
    private DeviceTypeService novaDeviceTypeService;

    private Log log = LogFactory.getLog(AreaPoint.class);

    /**
     * 综合经营分析表
     */
    @RequestMapping(value = "/operate")
    public ModelAndView operate(HttpServletRequest request,
                                HttpSession session, ModelMap modelMap) {

        return new ModelAndView("statistics/operate", modelMap);
    }

    /**
     * 用电分析表
     */
    @RequestMapping(value = "/onenergy")
    public ModelAndView onenergy(HttpServletRequest request,
                                 HttpSession session, ModelMap modelMap) {
        String customername = (String) session.getAttribute("customername");
        modelMap.put("customername", customername);
        return new ModelAndView("statistics/onenergy", modelMap);
    }

    /**
     * 通宵天数分析表
     */
    @RequestMapping(value = "/allnight")
    public ModelAndView allnight(HttpServletRequest request,
                                 HttpSession session, ModelMap modelMap) {

        return new ModelAndView("statistics/allnight", modelMap);
    }

    /**
     * 区域用电分析表
     */
    @RequestMapping(value = "/region")
    public ModelAndView region(HttpServletRequest request, HttpSession session,
                               ModelMap modelMap) {

        return new ModelAndView("statistics/region", modelMap);
    }

    /**
     * 节电分析表
     */
    @RequestMapping(value = "/compare")
    public ModelAndView compare(HttpServletRequest request,
                                HttpSession session, ModelMap modelMap) {

        return new ModelAndView("statistics/compare", modelMap);
    }

    /**
     * 网关月度统计表
     */
    @RequestMapping(value = "/gatewaymonth")
    public ModelAndView gatewayMonth(HttpServletRequest request,
                                     HttpSession session, ModelMap modelMap) {
        List<Map<String, Object>> list = novaAreapointServices.getAllGatewayCustomer();
        modelMap.put("gateways", JSON.toJSONString(list));
        return new ModelAndView("statistics/gatewaymonth", modelMap);
    }

    /**
     * 代理商返点分析表
     */
    @RequestMapping(value = "/agent")
    public ModelAndView agent(HttpServletRequest request, HttpSession session,
                              ModelMap modelMap) {

        return new ModelAndView("statistics/agent", modelMap);
    }

    // 获取某个单位每天的耗电量
    @RequestMapping(value = "findPowerEveryDay.shtml")
    @ResponseBody
    public List<Map<String, Object>> findPowerEveryDay(
            HttpServletRequest request, HttpSession session,
            Integer customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT);
        }
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        List<Map<String, Object>> maps = novaGatherSummaryService.getCostbydevice(customerinfoid, startDate, endDate);
        if (maps.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            map.put("customername", customerInfo.getCustomername());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            map1.put("customername", customerInfo.getCustomername());
            maps.add(0, map);
            maps.add(1, map1);
        }
        return maps;
    }

    // 获取某个单位一段时间内每个区域的耗电量
    @RequestMapping(value = "findPowerEveryRegion.shtml")
    @ResponseBody
    public List<Map<String, Object>> findPowerEveryRegion(
            HttpServletRequest request, HttpSession session,
            Integer customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT);
        }
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        List<Map<String, Object>> list = novaGatherSummaryService.getEnergyByRegion(
                customerinfoid, startDate, endDate);
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        if (list.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            map.put("customername", customerInfo.getCustomername());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            map1.put("customername", customerInfo.getCustomername());
            list.add(0, map);
            list.add(1, map1);
        }
        return list;
    }

    // 获取某个单位某段时间每天的LED、传统灯具耗电量
    @RequestMapping(value = "getAllEnergyByDate.shtml")
    @ResponseBody
    public List<Map<String, Object>> getAllEnergyByDate(
            HttpServletRequest request, HttpSession session,
            Integer customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_DAY, -7,
                    DateUtil.DEFAULT_DATE_FORMAT);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT);
        }
        if (customerinfoid == null || customerinfoid == 0) {
            customerinfoid = login.getCustomerinfoid();
        }
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerinfoid);
        DeviceType light = novaDeviceTypeService.getByName(ModelConstant.DEVICE_LIGHTENERGY);
        List<Map<String, Object>> list = novaGatherSummaryService.getAllEnergyByDate(
                customerinfoid, startDate, endDate, light.getId());
        if (list.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            map.put("customername", customerInfo.getCustomername());
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            map1.put("customername", customerInfo.getCustomername());
            list.add(0, map);
            list.add(1, map1);
        }
        return list;
    }

    // 获取某段时间每个单位的单位生产值、单位人员耗电量、单位面积耗电量
    @RequestMapping(value = "getUnitEnergyByDate.shtml")
    @ResponseBody
    public List<Map<String, Object>> getUnitEnergyByDate(
            HttpServletRequest request, HttpSession session,
            String customerinfoid, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_MONTH, -7,
                    DateUtil.DEFAULT_DATE_FORMAT_YM);
        }
        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT_YM);
        }
        if (StringUtils.isEmpty(customerinfoid)) {
            customerinfoid = String.valueOf(login.getCustomerinfoid());
        }
        List<Map<String, Object>> list = novaGatherSummaryService.getUnitEnergyByDate(
                customerinfoid, startDate, endDate);
        if (list.size() == 0) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsdate", startDate);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("statisticsdate", endDate);
            list.add(0, map);
            list.add(1, map1);
        }

        return list;
    }

    // 获取某段时间每个单位通宵供电的次数
    @RequestMapping(value = "getAllNightByDate.shtml")
    @ResponseBody
    public List<Map<String, Object>> getAllNightByDate(
            HttpServletRequest request, HttpSession session,
            String companyCode, String startDate, String endDate) {
        Login login = (Login) session.getAttribute("sessionLogin");
        if (StringUtil.isEmpty(startDate)) {
            startDate = DateUtil.getAddDate(DateUtil.DEFAULT_MONTH, -7,
                    DateUtil.DEFAULT_DATE_FORMAT_YM);
        }

        if (StringUtil.isEmpty(endDate)) {
            endDate = DateUtil.today(DateUtil.DEFAULT_DATE_FORMAT_YM);
        }
        if (StringUtils.isEmpty(companyCode)) {
            companyCode = String.valueOf(novaCustomerInfoService.getById(login.getCustomerinfoid()).getCompanycode());
        }
        List<Map<String, Object>> list = novaEnergyService.getAllNightByDate(
                companyCode, ModelConstant.MIN_TIME, ModelConstant.MAX_TIME,
                startDate, endDate);

        return list;
    }

    // 获取某个月份某个网关的详情数据
    @RequestMapping(value = "getGatewayDetail.shtml")
    @ResponseBody
    public List<Map<String, Object>> getGatewayDetail(
            HttpServletRequest request, HttpSession session,
            String companyCode, String searchDate, String gatewayId) {
        List<Map<String, Object>> list = novaGatherSummaryService.getGatewayDetail(searchDate, gatewayId);
        return list;
    }


}
